import { useEffect, useState } from "react";
import { DateTimePickerLang } from "src/components/DateTimePicker/DateTimePicker";
import { getDate, toDate } from "../uilts/handlerDate";

/**
 *
 * @param da new Date()
 * @param str 示例: YYYY-MM-DD hh:mm:ss = 2000-12-06 11:22:33
 * @returns
 */
export const useDate = (da: Date, format: string, lang: DateTimePickerLang) => {
  const [data] = useState(da);
  //#region 第一次进入月份加一
  useEffect(() => {
    data.setMonth(data.getMonth() + 1);
  }, []);
  //#endregion

  const [y, m, d, h, min, s] = getDate(data);

  const [showy, setY] = useState(y.currentY);
  const [showm, setM] = useState(m.currentM);
  const [showd, setD] = useState(d.currentD);
  const [showh, setH] = useState(h.currentH);
  const [showmin, setMin] = useState(min.currentMin);
  const [shows, setS] = useState(s.currentS);
  const res = {
    toDate() {
      return toDate(data, format);
    },
    y: {
      arr: y.ys,
      active: showy,
      isShow: format.includes("YYYY"),
      label: lang.y,
      set(num: number) {
        data.setFullYear(num);
        setY(num);
      }
    },
    m: {
      arr: m.ms,
      active: showm,
      label: lang.m,
      isShow: format.includes("MM"),
      set(num: number) {
        data.setMonth(num);
        setM(num);
      }
    },
    d: {
      arr: d.ds,
      active: showd,
      label: lang.d,
      isShow: format.includes("DD"),
      set(num: number) {
        data.setDate(num);
        setD(num);
      }
    },
    h: {
      arr: h.hs,
      active: showh,
      label: lang.h,
      isShow: format.includes("hh"),
      set(num: number) {
        data.setHours(num);
        setH(num);
      }
    },
    min: {
      arr: min.mins,
      active: showmin,
      label: lang.min,
      isShow: format.includes("mm"),
      set(num: number) {
        data.setMinutes(num);
        setMin(num);
      }
    },
    s: {
      arr: s.ss,
      active: shows,
      label: lang.s,
      isShow: format.includes("ss"),
      set(num: number) {
        data.setSeconds(num);
        setS(num);
      }
    }
  };
  return res;
};
